package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.common.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pojo.pro.WorkFlowProcess;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.WorkFlowProcessMapper;
import com.qidianit.mes.pro.service.WorkFlowProcessService;
import com.qidianit.mes.sys.mapper.DictDataMapper;
import com.qidianit.mes.vo.WorkFlowProcessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Package Path: com.qidianit.mes.pro.service.impl
 * Author: Alhz
 * Description:
 * Date: 2025/9/17 19:01
 */
@Service
public class WorkFlowProcessServiceImpl implements WorkFlowProcessService {
    @Autowired
    private WorkFlowProcessMapper workFlowProcessMapper;

    @Autowired
    private ProcessMapper processMapper;

    @Autowired
    private DictDataMapper dictDataMapper;


    /**
     * 查询工序组成
     * @param workflowId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result selectWorkFlowProcessList(Long workflowId , int page , int pageSize) {

        PageHelper.startPage(page, pageSize);
        List<WorkFlowProcessVo> workFlowPageVoList = workFlowProcessMapper.selectWorkFlowProcessList(workflowId);
        for(WorkFlowProcessVo workFlowProcessVo : workFlowPageVoList){
            //根据 processID 获取对应的工艺信息
            workFlowProcessVo.setProcess(processMapper.getProcessById(workFlowProcessVo.getProcessId()));


            //根据字典关系 查询对应的字典内容
            workFlowProcessVo.setRelationNextDict(dictDataMapper.selectDictDataById(workFlowProcessVo.getRelationNext()));

        }

        for(int i = 0; i <workFlowPageVoList.size() - 1 ; i++){
            workFlowPageVoList.get(i).setNextProcess(workFlowPageVoList.get(i + 1).getProcess());
        }

        PageInfo<WorkFlowProcessVo> pageInfo = new PageInfo<>(workFlowPageVoList);
        return Result.success(pageInfo);
    }

    /**
     * 新增工序组成
     * @param workFlowProcess
     * @return
     */
    @Override
    public Result addWorkFlowProcess(WorkFlowProcess workFlowProcess) {
        workFlowProcess.setCreateTime(LocalDateTime.now());
        workFlowProcess.setUpdateTime(LocalDateTime.now());


         //判断工序编号是否重复（排除当前记录）
        if(workFlowProcessMapper.selectWorkFlowProcessByWorkFlowIdAndStepOrderExcludeId(
            workFlowProcess.getWorkflowId(), 
            workFlowProcess.getStepOrder(), 
            workFlowProcess.getId()) > 0){
            return Result.error("工序序号["+workFlowProcess.getStepOrder()+"]已经存在");
        }

        //判断工序是否存在
        Process process = processMapper.getProcessById(workFlowProcess.getProcessId());
        if(process == null){
            return Result.error("工序不存在，请检查工序ID["+workFlowProcess.getProcessId()+"]");
        }


        return workFlowProcessMapper.addWorkFlowProcess(workFlowProcess)>0?Result.success():Result.error("操作失败");
    }


    /**
     * 根据id获取工序组成
     * @param id
     * @return
     */
    @Override
    public Result getWorkFlowProcessById(Long id) {
        return Result.success(workFlowProcessMapper.getWorkFlowProcessById(id));
    }


    /**
     * 更新工序组成
     * @param workFlowProcess
     * @return
     */
    @Override
    public Result updateWorkFlowProcess(WorkFlowProcess workFlowProcess) {
        workFlowProcess.setUpdateTime(LocalDateTime.now());

        //判断工序编号是否重复（排除当前记录）
        if(workFlowProcessMapper.selectWorkFlowProcessByWorkFlowIdAndStepOrderExcludeId(
                workFlowProcess.getWorkflowId(), 
                workFlowProcess.getStepOrder(), 
                workFlowProcess.getId()) > 0){
            return Result.error("工序序号["+workFlowProcess.getStepOrder()+"]已经存在");
        }

        //判断工序是否存在
        Process process = processMapper.getProcessById(workFlowProcess.getProcessId());
        if(process == null){
            return Result.error("工序不存在，请检查工序ID["+workFlowProcess.getProcessId()+"]");
        }

        return workFlowProcessMapper.updateWorkFlowProcess(workFlowProcess)>0?Result.success():Result.error("操作失败");
    }

    /**
     * 根据id删除工序组成
     * @param id
     * @return
     */
    @Override
    public Result deleteWorkFlowProcessById(Long id) {
        return workFlowProcessMapper.deleteWorkFlowProcessById(id)>0?Result.success():Result.error("操作失败");
    }

    /**
     * 批量删除工序组成
     * @param ids
     * @return
     */
    @Override
    public Result batchDeleteWorkFlowProcess(Long[] ids) {
        return workFlowProcessMapper.batchDeleteWorkFlowProcess(ids)>0?Result.success():Result.error("操作失败");
    }
}
